// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_initx.sqf"
#include "x_setup.sqf"
if (!isServer) exitWith{};

__ccppfln(x_shc\x_shcinit.sqf);

//TODO no warfare stuff in A3
#ifndef __TT__
0 spawn {
	scriptName "spawn_x_initx_createbase";
	waitUntil {time > 0};
	sleep 2;
	private ["_vecs","_radar","_mmm","_ammop","_mpos","_mdir","_dgrp","_ret","_vec"];
	_radar = createVehicle [GVAR(baseAARadarUS), GVAR(base_radar_pos), [], 0, "NONE"];
	_radar setDir (markerDir QGVAR(base_radar_pos));
	_radar setPos GVAR(base_radar_pos);
	_radar setVectorUp [0,0,1];
	_radar addMPEventHandler ["MPkilled", {if (call FUNC(checkSHC)) then {GVAR(banti_airdown) = true};if (isServer) then {_this call FUNC(radarkilled);(_this select 0) removeAllMPEventHandlers "MPKilled"}}];
	
	_mmm = markerPos QGVAR(base_fac_ammoload);
	__TRACE_1("","_mmm"); 
	
	_ammop = createVehicle [GVAR(WarfareAircraftFactory_Blufor), _mmm, [], 0, "NONE"];
	_ammop setDir (markerDir QGVAR(base_fac_ammoload));
	_ammop setPos _mmm;
	_ammop addEventHandler ["handleDamage", {0}];
	deleteMarker QGVAR(base_radar_pos);
	
	if (isNil QGVAR(HC_CLIENT_OBJ)) then {
		_vecs = [];
		for "_io" from 1 to 2 do {
			_mmm = format [QGVAR(base_anti_air%1), _io];
			_mpos = markerPos _mmm;
			_mpos set [2,0];
			_mdir = markerDir _mmm;
		
			_dgrp = [GVAR(own_side)] call FUNC(creategroup);
			_ret = [1, _mpos, "B_APC_Tracked_01_AA_F", _dgrp, 0, _mdir] call FUNC(makevgroup);
			((_ret select 0) select 0) lock true;
			// TYRGHEN
			_vec = ((_ret select 0) select 0);
			(group _vec) setBehaviour "COMBAT";
			(group _vec) setCombatMode "RED";
			(driver _vec) disableAI "MOVE";
			_vec addEventHandler ["HandleDamage",{0}];
			{ _x addEventHandler ["HandleDamage",{0}];
			  _x setSkill 1;
			} foreach crew _vec;
			_vecs set[count _vecs, _vec];
		};
		_vecs spawn { private "_vecs"; _vecs = _this; while {{alive _x} count _vecs > 0} do { {[_x] call FUNC(reloadInstant);} foreach _vecs; sleep 347.345; }};
	} else {
		[QGVAR(cgraa), [GVAR(HC_CLIENT_OBJ), GVAR(own_side), GVAR(base_aa_type)]] call FUNC(NetCallEventSTO);
	};
};
#endif

if (true) exitWith {};

#ifndef __TT__
0 spawn {
	scriptName "spawn_x_initx_createbase";
	waitUntil {time > 0};
	sleep 2;
	private ["_x_objs", "_x_objs2", "_x_objs3", "_dgrp", "_unit_array"];
	_x_objs = [];
	_x_objs2 = [];
	_x_objs3 = [];
	_radar = "";
	if (GVAR(own_side) == "BLUFOR") then {
		_radar = GVAR(baseAARadarUS);
		_x_objs = [GVAR(base_radar_pos), random 0, ["radar","us_army"], 0, [GVAR(ClamShell),GVAR(baseAARadarUS)]] call FUNC(objectMapper);
	} else {
		_radar = GVAR(baseAARadaropfor);
		_x_objs = [GVAR(base_radar_pos), random 0, ["radar","tk_army"], 0, [GVAR(ClamShell),GVAR(baseAARadaropfor)]] call FUNC(objectMapper);
	};

	if (isNil QGVAR(HC_CLIENT_OBJ)) then {
		_dgrp = [GVAR(own_side)] call FUNC(creategroup);
		_unit_array = ["basic", GVAR(own_side)] call FUNC(getunitliste);
		[GVAR(base_radar_pos), _unit_array select 0, _dgrp,true] call FUNC(makemgroup);
		[_dgrp, GVAR(base_radar_pos)] call FUNC(taskDefend);
	} else {
		[QGVAR(cgrd), [GVAR(HC_CLIENT_OBJ), GVAR(own_side), "basic", GVAR(base_radar_pos)]] call FUNC(NetCallEventSTO);
	};

	if (GVAR(own_side) == "BLUFOR") then {
		_x_objs2 = [GVAR(base_anti_air1), random 0, ["anti-air","us_army"]] call FUNC(objectMapper);
	} else {
		_x_objs2 = [GVAR(base_anti_air1), random 0, ["anti-air","tk_army"]] call FUNC(objectMapper);
	};

	if (isNil QGVAR(HC_CLIENT_OBJ)) then {
		_dgrp = [GVAR(own_side)] call FUNC(creategroup);
		_unit_array = ["basic", GVAR(own_side)] call FUNC(getunitliste);
		[GVAR(base_anti_air1), _unit_array select 0, _dgrp,true] call FUNC(makemgroup);
		[_dgrp, GVAR(base_anti_air1)] call FUNC(taskDefend);
	} else {
		[QGVAR(cgrd), [GVAR(HC_CLIENT_OBJ), GVAR(own_side), "basic", GVAR(base_anti_air1)]] call FUNC(NetCallEventSTO);
	};

	if (GVAR(own_side) == "BLUFOR") then {
		_x_objs3 = [GVAR(base_anti_air2), random 0, ["anti-air","us_army"]] call FUNC(objectMapper);
	} else {
		_x_objs3 = [GVAR(base_anti_air2), random 0, ["anti-air","tk_army"]] call FUNC(objectMapper);
	};

	if (isNil QGVAR(HC_CLIENT_OBJ)) then {
		_dgrp = [GVAR(own_side)] call FUNC(creategroup);
		_unit_array = ["basic", GVAR(own_side)] call FUNC(getunitliste);
		[GVAR(base_anti_air2), _unit_array select 0, _dgrp,true] call FUNC(makemgroup);
		[_dgrp, GVAR(base_anti_air2)] call FUNC(taskDefend);
	} else {
		[QGVAR(cgrd), [GVAR(HC_CLIENT_OBJ), GVAR(own_side), "basic", GVAR(base_anti_air2)]] call FUNC(NetCallEventSTO);
	};

	{
		_rbox = false;
		if (GVAR(with_ranked) && {_x isKindOf "ReammoBox_F"}) then {
			deleteVehicle _x;
			_rbox = true;
		};
		if (!_rbox) then {
			if !(_x isKindOf _radar) then {
				__addDead(_x)
				if ((toUpper (typeOf _x)) in GVAR(base_aavecs_check)) then {
					_x addEventHandler ["killed", {_this call FUNC(handleDeadVec)}];
					if (isNil QGVAR(HC_CLIENT_OBJ)) then {
						_dgrp = [GVAR(own_side)] call FUNC(creategroup);
						[_x, _dgrp] call FUNC(spawnCrew);
						_x lock true;
					} else {
						[QGVAR(spcr), [GVAR(HC_CLIENT_OBJ), GVAR(own_side), _x]] call FUNC(NetCallEventSTO);
					};
				};
			} else {
				_x addMPEventHandler ["MPkilled", {if (call FUNC(checkSHC)) then {GVAR(banti_airdown) = true};if (isServer) then {_this call FUNC(radarkilled)}}];
			};
		};
	} forEach _x_objs + _x_objs2 + _x_objs3;
};
#endif
